package com.erp.erp_ui.email;

import com.erp.erp_entitys.Email.email_Pop3;
import com.erp.erp_entitys.Email.email_email;

import javax.mail.Folder;
import javax.mail.MessagingException;
import javax.mail.Store;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

//连接器对象
public class Link {
    private TreeMap<Integer, Store> stores=new TreeMap<>();
//    private TreeMap<Integer, Folder> folders=new TreeMap<>();
    private TreeMap<Integer, email_email> emails=new TreeMap<>();

    public Folder openFolder(email_Pop3 pop) throws MessagingException {
        Store store = getStore(pop);
        Folder folder = store.getFolder("INBOX");//获取收件箱
        folder.open(Folder.READ_WRITE); // 设置对邮件帐户的访问权限
        return  folder;
    }

    //新增连接器
    public boolean addLink(email_Pop3 pop3) throws MessagingException {
        if(stores.containsKey(pop3.getPop3_id())==false){
            Store store = EmailUnit.getStore(pop3);
            stores.put(pop3.getPop3_id(),store);
//            Folder folder = store.getFolder("INBOX");//获取收件箱
//            folder.open(Folder.READ_WRITE); // 设置对邮件帐户的访问权限
//            folders.put(pop3.getPop3_id(),folder);
        }else{
            return false;
        }
        return true;
    }

    //删除连接池
    public boolean deleteLink(email_Pop3 pop3) throws MessagingException {
        if(stores.containsKey(pop3.getPop3_id())){
            Store store = stores.get(pop3.getPop3_id());
//            Folder folder = folders.get(pop3.getPop3_id());
            store.close();
//            folder.close();
        }else{
            return false;
        }
        return true;
    }

    //获取连接器
    public Store getStore(email_Pop3 pop3) throws MessagingException {
        Store store=null;
        if(stores.containsKey(pop3.getPop3_id())){
            if(stores.get(pop3.getPop3_id()).isConnected()){
                store = stores.get(pop3.getPop3_id());
            }else{
                try{
                    stores.get(pop3.getPop3_id()).close();
                }catch (Exception e){}
                stores.remove(pop3.getPop3_id());
                addLink(pop3);
                store=stores.get(pop3);
            }
            return store;
        }else{
            addLink(pop3);
           return stores.get(pop3.getPop3_id());
        }

    }
    //获取收件箱
    public Folder getFolder(email_Pop3 pop3) throws MessagingException {
        //可能丢失连接
        Folder folder = openFolder(pop3);
        return folder;

//        try{
//            if(stores.containsKey(pop3.getPop3_id())){
//                Folder folder = folders.get(pop3.getPop3_id());
//                return folder;
//            }else{
//                addLink(pop3);
//                return folders.get(pop3.getPop3_id());
//            }
//        }catch (Exception e){
//                try {
//                    closeOne(pop3);
//                }catch (Exception a){
//                    System.out.println("出错");
//                }
//                stores.remove(pop3.getPop3_id());
//                folders.remove(pop3.getPop3_id());
//                addLink(pop3);
//                return  folders.get(pop3.getPop3_id());
//        }
    }

    //关闭所有连接池
    public void closeAll() throws MessagingException {
        try{
            while(stores.size()==0){
                Map.Entry<Integer, Store> entry = stores.lastEntry();
                entry.getValue().close();
                stores.remove(entry.getKey());
            }
//            while(folders.size()==0){
//                Map.Entry<Integer, Folder> entry = folders.lastEntry();
//                entry.getValue().close();
//                folders.remove(entry.getKey());
//            }

        }catch (Exception e){
            System.out.println("剩余stores为"+stores.size());
//            System.out.println("剩余folders为"+folders.size());
        }finally {
            stores.clear();
//            folders.clear();
        }
    }
    //关闭单个连接
    public void closeOne(email_Pop3 pop) throws MessagingException {
        Store store = stores.get(pop.getPop3_id());
//        Folder folder = folders.get(pop.getPop3_id());
        store.close();
//        folder.close();
    }

    //新增邮件单个
    public boolean addEmail(email_email email){
       if( emails.containsKey(email.getEmail_id())==false){
           emails.put(email.getEmail_id(),email);
           return true;
       }else{
           return false;
       }
    }
    //新增集合
    public void addEmail(List<email_email> emails){
        for(email_email email:emails){
            addEmail(email);
        }
    }

    //删除邮件
    public boolean deleteEmail(email_email email){
        if(emails.containsKey(email.getEmail_id())){
            emails.remove(email.getEmail_id());
            return true;
        }else{
            return false;
        }
    }
    public email_email getRamEmail(){
        if(emails.size()>0){
            return emails.lastEntry().getValue();
        }else{
            return null;
        }

    }
    public int getEmailSize(){
        return emails.size();
    }
}
